﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace hozoroghiab
{
    public partial class editestaff : Form
    {
        public sql sqlquery;
        DataTable table;
        SqlDataAdapter da;
        DataGridViewButtonColumn deleteButton, editeButton;
        BindingSource bs = new BindingSource();
        bool addbutton = false;
        public editestaff(sql s)
        {
            InitializeComponent();
            sqlquery = s;
        }


        private void editestaff_Load(object sender, EventArgs e)
        {


            setheader();

        }

        void setheader()
        {
            
            dataGridView1.Columns[2].HeaderText = "شماره کارمندی";
            dataGridView1.Columns[3].HeaderText = "نام";
            dataGridView1.Columns[4].HeaderText = "نام خانوادگی";
            dataGridView1.Columns[5].HeaderText = "واحد";
            dataGridView1.Columns[6].HeaderText = "جانباز";
            dataGridView1.Columns[7].HeaderText = "نگهبان";
            dataGridView1.Columns[8].HeaderText = "حضور قبل از شروع کار";
            dataGridView1.Columns[9].HeaderText = "حضور بعد از کار";
            dataGridView1.Columns[10].HeaderText = "بدون اضافه کاری";
            dataGridView1.Columns[11].HeaderText = "نرخ اضافه کاری";
            dataGridView1.Columns[12].HeaderText = "نرخ پایه";

        }



        private void editestaff_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void editestaff_FormClosed(object sender, FormClosedEventArgs e)
        {
            /* DialogResult dr = MessageBox.Show("آیا می خواهید تغیرات اعمال شود", "توجه", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
             if (dr == DialogResult.Yes)
             {
                 da.Update(table);
                 Close();
             }
             Close();*/
        }

        private void save_btn_Click(object sender, EventArgs e)
        {
            try
            {
                sqlquery.open();
                string q = "INSERT INTO profile (employeid,name,family,unit,janbaz,negahban,bf_extra,af_extra,extra,rate_out,fix_extra)VALUES(" + textBox1.Text.Trim() + "," + textBox2.Text.Trim() + "," + textBox3.Text.Trim() + "," + textBox4.Text.Trim() + "," + checkBox1.Checked + "," + checkBox4.Checked + "," + checkBox5.Checked + "," + checkBox2.Checked + "," + checkBox3.Checked + "," + textBox8.Text.Trim() + "," + textBox7.Text.Trim() + ")";
                sqlquery.ExecuteNonQuery(q);
                sqlquery.close();
            }
            catch
            {
                MessageBox.Show("اطلاعات را درست وارد نکرده اید");
            }
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            checkBox5.Checked = false;

        }
        
        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                sqlquery.open();
                da = sqlquery.sqlcommand("SELECT TOP 100 * FROM profile");
                table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                da.Fill(table);
                bs.DataSource = table;
                dataGridView1.DataSource = bs;
                sqlquery.close();
            }
            catch
            {
                MessageBox.Show("مشکلی رخ داده است");
            }
            setheader();
            nametextbox.Text = "";
            familytextbox.Text = "";

        }

        private void familytextbox_TextChanged_1(object sender, EventArgs e)
        {
            sqlquery.open();
            da = sqlquery.sqlcommand("SELECT * FROM profile WHERE family like N'%" + familytextbox.Text.Trim() + "%'");
            table = new DataTable();
            da.Fill(table);
            bs.DataSource = table;
            dataGridView1.DataSource = bs;
            sqlquery.close();
            setheader();
        }

        private void nametextbox_TextChanged_1(object sender, EventArgs e)
        {
            sqlquery.open();
            da = sqlquery.sqlcommand("SELECT * FROM profile WHERE name like N'%" + nametextbox.Text.Trim() + "%'");
            table = new DataTable();
            da.Fill(table);
            bs.DataSource = table;
            dataGridView1.DataSource = bs;
            sqlquery.close();
            setheader();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            try
            {
                da.Update((DataTable)bs.DataSource);
            }
            catch
            {
                MessageBox.Show("نام ها را صحیح وارد کنید");
            }
            nametextbox.Text = "";
            familytextbox.Text = "";

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            int currentRow = int.Parse(e.RowIndex.ToString());

            if (dataGridView1.Columns[e.ColumnIndex] == deleteButton && currentRow >= 0)
            {
                DialogResult msg= MessageBox.Show("Are You Sure?","Delete",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
                if (msg == DialogResult.OK)
                {
                    sqlquery.open();
                    sqlquery.ExecuteNonQuery("DELETE profile WHERE employeid="+dataGridView1[2,currentRow].Value);
                    sqlquery.close();
                    dataGridView1.Rows.RemoveAt(currentRow);
                }

            }
            else if (dataGridView1.Columns[e.ColumnIndex] == editeButton && currentRow >= 0)
            {

            }
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (!addbutton)
            {
                deleteButton = new DataGridViewButtonColumn();
                deleteButton.HeaderText = "حذف";
                deleteButton.Text = "حذف";
                deleteButton.UseColumnTextForButtonValue = true;
                deleteButton.Width = 50;
                dataGridView1.Columns.Add(deleteButton);

                editeButton = new DataGridViewButtonColumn();
                editeButton.HeaderText = "ویرایش";
                editeButton.Text = "ویرایش";
                editeButton.UseColumnTextForButtonValue = true;
                editeButton.Width = 50;
                dataGridView1.Columns.Add(editeButton);

                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.ReadOnly = true;
                addbutton = true;
            }
        }
    }
}
